package com.situ.player.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.situ.player.R;
import com.situ.player.activity.MainActivity;
import com.situ.player.activity.MusicPlayActivity;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

// Adapter是适配器，数据和界面绑定
public class FileAdapter extends BaseAdapter {
    private MainActivity mainActivity;
    private File[] files;

    public FileAdapter(MainActivity mainActivity) {
        this.mainActivity = mainActivity;
    }

    @Override
    public int getCount() {
        return files.length;
    }

    @Override
    public Object getItem(int position) {
        return files[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    // 处理Data-View填充数据过程
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHodler viewHodler = new ViewHodler();
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) mainActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.list_item, null);
            viewHodler.ivFileIcon = convertView.findViewById(R.id.iv_file_icon);
            viewHodler.tvFileName = convertView.findViewById(R.id.tv_file_name);
            // 第一次保存起来
            convertView.setTag(viewHodler);
        }  else {
            // 不是第一次直接拿出来
            viewHodler = (ViewHodler) convertView.getTag();
        }

        File file = files[position];
        String fileName = file.getName();// android.mp3
        viewHodler.tvFileName.setText(fileName);
        if (file.isDirectory()) {
            viewHodler.ivFileIcon.setBackgroundResource(R.drawable.icon_folder);
        } else {
            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);// mp3
            if ("mp3".equalsIgnoreCase(suffix)) {
                viewHodler.ivFileIcon.setBackgroundResource(R.drawable.icon_audio);
            } else if ("mp4".equalsIgnoreCase(suffix)) {
                viewHodler.ivFileIcon.setBackgroundResource(R.drawable.icon_video);
            } else {
                viewHodler.ivFileIcon.setBackgroundResource(R.drawable.icon_file);
            }
        }

        // 给每一行添加一个点击事件
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (file.isDirectory()) {
                    files = file.listFiles();
                    // 如果更改了Adapter中的数据，需要调用
                    notifyDataSetChanged();
                    // 当点击的下一层目录，要更新MainActivity中保存的当前目录
                    mainActivity.updatePath(file);
                } else {
                    String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);// mp3
                    if ("mp3".equalsIgnoreCase(suffix)) {
                        Intent intent = new Intent(mainActivity, MusicPlayActivity.class);
                        intent.putExtra("path", file.getAbsolutePath());
                        ArrayList<String> mp3List = getMP3Files(files);
                        intent.putExtra("mp3List", mp3List);
                        mainActivity.startActivity(intent);
                    } else if ("mp4".equalsIgnoreCase(suffix)) {
                    } else {
                    }
                }
            }
        });

        return convertView;
    }

    /**
     * 返回当前目录下面所有的mp3歌曲
     * @param files
     * @return
     */
    private ArrayList<String> getMP3Files(File[] files) {
        ArrayList<String> list = new ArrayList<>();
        for (File file : files) {
            String fileName = file.getName();
            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
            if ("mp3".equalsIgnoreCase(suffix)) {
                list.add(file.getAbsolutePath());
            }
        }

        return list;
    }

    public void updateData(File file) {
        this.files = file.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return !pathname.getName().startsWith(".");
            }
        });
        sortData(files);
        notifyDataSetChanged();
    }

    private void sortData(File[] files) {
        Collections.sort(Arrays.asList(files), new Comparator<File>() {
            @Override
            public int compare(File file1, File file2) {
                if (file1.isDirectory() && file2.isFile()) {
                    return -1;
                } else if (file1.isFile() && file2.isDirectory()) {
                    return 1;
                } else {
                    // file1和file2要么同时是文件夹要么同时是文件
                    // 字符串已经默认实现了compareTo，按照字母顺序排列
                    return file1.getName().compareTo(file2.getName());
                }
            }
        });
    }

    public class ViewHodler{
        public ImageView ivFileIcon;
        public TextView tvFileName;
    }
}
